﻿using CKS.Core.Framework;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP140
{
    /// <summary>
    /// 动作编号1
    /// 1号钎焊料架放满上传请求
    /// </summary>
    public class DB3011_Work01Action : PLCAction
    {
        public DB3011_Work01Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var result = Value.Value.ToInt();
            var replayDB = 3010;
            var replayAction = 10;
            if (result == 1)
            {

                DcOp140Record dc = new DcOp140Record()
                {
                    CreatedTime = DateTime.Now,
                    ProductCode = Value.GetLinkData(1).ToString(),
                    TotalResult = Value.GetLinkData(2).ToInt(),
                    StopGaugeResult1 = Value.GetLinkData(3).ToInt(),
                    StopGaugeResult2 = Value.GetLinkData(4).ToInt(),
                    GoGaugeResult1 = Value.GetLinkData(5).ToInt(),
                    GoGaugeResult2 = Value.GetLinkData(6).ToInt(),
                    InspectionCyclesOfStopGauge1 = Value.GetLinkData(7).ToDecimal(),
                    InspectionCyclesOfStopGauge2 = Value.GetLinkData(8).ToDecimal(),
                    GoGaugeDetectCycles1 = Value.GetLinkData(9).ToDecimal(),
                    GoGaugeDetectCycles2 = Value.GetLinkData(10).ToDecimal(),

                };



                CatchAction(() => {
                    db.Insertable(dc).ExecuteCommand();
                }, "OP140请求保存数据");

                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP140请求保存数据{dc.ProductCode}");
            }
            else
            {
                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog("OP140 重置保存状态");
            }
        }
    }
}
